package com.appcan.sync.common;

import java.util.Enumeration;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;

public class SchedulerMaintainer {

	private static final Log log = LogFactory.getLog(SchedulerMaintainer.class);

	public static void startScheduler() {
		try {
			SchedulerUtil.loadInitProperties();
			if(!SchedulerUtil.getQuertz_job_properties().isEmpty()){
				Enumeration<?> enu = 	SchedulerUtil.getQuertz_job_properties().propertyNames();
				while (enu.hasMoreElements()) {
					String key = (String)enu.nextElement();
					Scheduler scheduler = SchedulerLoader.getSchedulerInst();
					String type = SchedulerUtil.getQuertz_type_properties().getProperty(key);
					JobDetail jobdetail = new JobDetail(key, key + "_GROUP", Class.forName(SchedulerUtil.getQuertz_job_properties().getProperty(key)));
					if(StringUtils.isNotBlank(type)){
						JobDataMap dataMap = new JobDataMap();
						dataMap.put("type", type);
						jobdetail.setJobDataMap(dataMap);
					}
					CronTrigger trigger = new CronTrigger(key + "_TRIGGER", null, SchedulerUtil.getQuertz_trigger_properties().getProperty(key));
					scheduler.scheduleJob(jobdetail, trigger);
					scheduler.start();
				}
			}
//			Scheduler scheduler = SchedulerLoader.getSchedulerInst();
//			JobDetail jobdetail = SchedulerUtil.buildJobDetailByName("sync_general");
//			Trigger trigger = SchedulerUtil.buildTriggerByJobName("sync_general");
//			scheduler.scheduleJob(jobdetail, trigger);
//			scheduler.start();
//			
//			Scheduler scheduler1 = SchedulerLoader.getSchedulerInst();
//			JobDetail jobdetail1 = SchedulerUtil.buildJobDetailByName("sync_allot");
//			Trigger trigger1 = SchedulerUtil.buildTriggerByJobName("sync_allot");
//			scheduler1.scheduleJob(jobdetail1, trigger1);
//			scheduler1.start();
//			
//			Scheduler scheduler2 = SchedulerLoader.getSchedulerInst();
//			JobDetail jobdetail2 = SchedulerUtil.buildJobDetailByName("sync_special");
//			Trigger trigger2 = SchedulerUtil.buildTriggerByJobName("sync_special");
//			scheduler2.scheduleJob(jobdetail2, trigger2);
//			scheduler2.start();
//			
//			Scheduler scheduler3 = SchedulerLoader.getSchedulerInst();
//			JobDetail jobdetail3 = SchedulerUtil.buildJobDetailByName("sync_updatesql");
//			Trigger trigger3 = SchedulerUtil.buildTriggerByJobName("sync_updatesql");
//			scheduler3.scheduleJob(jobdetail3, trigger3);
//			scheduler3.start();
//			Scheduler scheduler4 = SchedulerLoader.getSchedulerInst();
//			JobDetail jobdetail4 = SchedulerUtil.buildJobDetailByName("sync_sale");
//			Trigger trigger4 = SchedulerUtil.buildTriggerByJobName("sync_sale");
//			scheduler4.scheduleJob(jobdetail4, trigger4);
//			scheduler4.start();
		}
		catch (Exception e) {
			log.error( e.toString());
		}
	}
	
}
